<?php 

#	Fastfoo - CMS developer under CakePHP / http://fast-foo.com
#	Copyright (C) 2010  Antonio Irlandés García

#	This program is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.

#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.

#	You should have received a copy of the GNU General Public License
#	along with this program.  If not, see <http://www.gnu.org/licenses/>.

class Backup extends AppModel{
	
	//Nombre modelo
	public $name = 'Backup';
	
	//Nombre tabla BD
	public $useTable = false;

	//ruta de la carpeta que contiene todos los archivos subidos y deseamos almacenar
	private $_path_files = "files/";

	//ruta donde se almacena el backup antes de ser descargado
	private $_path_backup = "/tmp/backup";

	//nombre del fichero SQL que contiene la copia de la BD
	private $_name_file_sql = "backup.sql";

	//nombre del fichero empaquetado que contiene el backup del sistema
	private $_name_file_backup;

	/**
	 * Genera Backup de la BD y almacena carpeta files, donde se subieron archivos
	 *
	 * @return void
	 */
	public function makeBackup(){

		//Copia los ficheros a guardar en carpeta temporal
		$this->_backupFiles();
	
		//genera fichero SQL con BACKUP de la BD
		$this->_backupDB();

		//Empaquetamos ambas cosas
		$this->_backupTar();

		//Descargamos backup
		$this->_backupDownload();

    }


	/**
	 * Realiza copia de la carpeta $_path_files en $_path_backup
	 *
	 * @return void
	 */
	private function _backupFiles(){
		//Generamos carpeta donde se almacenaran los archivos
		if(!is_dir($this->_path_backup))
			mkdir($this->_path_backup,0777);

		//Copiamos el contenido de la carpeta files
		shell_exec("cp -r ".ROOT.DS.APP_DIR.DS.WEBROOT_DIR.DS.$this->_path_files."* ".$this->_path_backup);

    }

	/**
	 * Genera Backup de la BD (mysqldump)
	 *
	 * @return void
	 */
	private function _backupDB(){
		if (class_exists('DATABASE_CONFIG')) {
			$configDataDb =& new DATABASE_CONFIG();
			shell_exec("mysqldump --opt --password=".$configDataDb->default['password']." --user=".$configDataDb->default['login']." ".$configDataDb->default['database']." > ".$this->_path_backup.DS.$this->_name_file_sql);
		}

    }

	/**
	 * Empaqueta todos los ficheros 
	 *
	 * @return void
	 */
	private function _backupTar(){
		$this->_name_file_backup = "backup-".date("dmYHis").".tar.gz";
		shell_exec("tar -cvzf /tmp/".$this->_name_file_backup." ".$this->_path_backup);

    }

	/**
	 * Prepara para descargar los ficheros
	 *
	 * @return void
	 */
	private function _backupDownload(){
	    header("Content-type: application/octet-stream");
    	header("Content-Disposition: attachment; filename=\"".$this->_name_file_backup."\"");
    	$fp = fopen("/tmp/".$this->_name_file_backup, "r");
    	fpassthru($fp);
		exit();
    }
}
?>
